#table A6

#directory
#set working directory to the path PvP_Replication
#setwd("~/PvP_Replication")

#packages
library(tidyverse) #for data cleaning and manipulation
library(texreg) #generate latex table 

#load main dataset
pvp_data <- read.csv("PvP_data/PvP_data_main.csv")

#transform independent variable
#create binary (hectares > 100) and log + 1 cultivation variables 
pvp_data <- pvp_data %>% 
  mutate(logcult = log(maxcult + 1), cultbinary = ifelse(maxcult > 100, 1, 0))

#subset data to municipalities with prior FARC presence
pvp_farc_subset <- pvp_data %>% filter(farc_presence == 1)

#transform selected weather and soil variables
pvp_farc_subset <- pvp_farc_subset %>% mutate(
  draindummy = as.factor(ifelse(DRAIN == "M", 1, 0)), #moderate drainage
  humiddist = case_when(humid_mean < 85 ~ (85 - humid_mean)^2, humid_mean > 85 ~ (humid_mean - 85)^2, TRUE ~ 0 ), #distance from optimal humidity point
  temprangedist = case_when(maxtemp > 27 ~ (maxtemp - 27)^2, mintemp < 14  ~ (14 - mintemp)^2, TRUE ~ 0), #distance from optimal temp, wide band
)

#run models where outcome is cocaine cultivation and independent variables are weather and soil
ols_inst_model <- lm(logcult ~ PHAQ + temprangedist + TOTN + TOTC + sun_mean + humiddist + raintot_me + draindummy, pvp_farc_subset)
glm_inst_model <- glm(cultbinary ~ PHAQ + temprangedist + TOTN + TOTC + sun_mean + humiddist + raintot_me + draindummy, pvp_farc_subset, family = binomial(link = "logit"))

#latex table for result
texreg(list(ols_inst_model, glm_inst_model), 
       digits = 3, include.ci = FALSE, include.aic = FALSE,
       include.bic = FALSE, include.loglik = FALSE,
       include.deviance = FALSE, include.rsquared = FALSE,
       include.adjrs = FALSE, stars = c(0.001, 0.01, 0.05), 
       caption.above	= TRUE, single.row = FALSE, 
       custom.coef.names	= c(NA, "Soil PH", "Temperature Range", "Soil Nitrogen", "Soil Carbon",
                             "Sunlight Hours", "Humidity Range", "Annual Rainfall", "Soil Drainage"),
       caption = "Coefficients for Regression-based Instrument Construction") 
